define(function (require, exports, module) {
    'use strict';
    //这个是将商品活动进行汇总以后的数据，纬度是活动本身
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common');

    require('multiselect');

    var ListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'change #inputCategory': 'changeCategory',
            'change #inputMode': 'changeMode',
            'click #paint': 'doPaint',
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.ActivityReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options = utils.getAllInput('#searchBox');
            this.options.date_range = helper.getDateRange();

            this.load();
        },
        doPaint: function () {
            $('#chart').empty();
            var column = $('#inputColumn').val(),
                name = $('#inputColumn :selected').text();
            if ([0, 1].indexOf(this.options.mode) != -1) {
                //饼图
                var options = chart.getHighChartPieOptions(),
                    that = this;
                _.each(that.model.list, function (v) {
                    options.series[0].data.push({
                        name: GOODS_CUSTUM_CATEGORY[v._id],
                        y: v[column]
                    });
                });
                $('#chart').highcharts(options);
            } else {
                //折线图
                var options = chart.getHighChartLineOptions(),
                    that = this;
                options.series.push({
                    name: '',
                    data: []
                });
                options.series[0].name = name;
                _.each(that.model.list, function (v) {
                    options.xAxis.categories.push(v.day);
                    options.series[0].data.push(v[column]);
                });
                $('#chart').highcharts(options);
            }
        },
        changeCategory: function (e) {
            if ($('#inputCategory').val() != '') {
                $('#inputMode').val('2').prop('disabled', true);
            } else {
                $('#inputMode').prop('disabled', false);
            }
        },
        changeMode: function () {
            if ($('#inputMode').val() != 2) {
                $('#inputCategory').val('').prop('disabled', true);
            } else {
                $('#inputCategory').prop('disabled', false);
            }
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
            }));
            helper.loadDatePicker({
                dateRange: this.options.date_range
            });

            return this;
        },
        load: function () {
            var data = {};
            _.each(this.options, function (v, k) {
                if (k[0] == '$') return;
                if (k == 'date_range') data[k] = v.join(',');
                else if ($.isArray(v) && v.length > 0) {
                    if (v.length == 1) data[k] = v[0];
                    else data[k] = JSON.stringify({ '$in': v });
                } else {
                    data[k] = v;
                }
            });
            this.model.fetch({
                reset: true,
                data: data,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/DailyActivity.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new ListView({
                    date_range: dateRange,
                    mode: 2
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });
});